#include "include/HttpClient.h"
using namespace hv;

int main() 
{
    // 创建Client
    int ret;
    http_client_t *cli = http_client_new(NULL, 8080, 1); // 这里参数填什么无所谓
    if(cli == NULL) {
        printf("Error: cli is null\n");
        return 0;
    }

    // 设置客户端证书
    hssl_ctx_opt_t *ssl_opt = new hssl_ctx_opt_t;
    ssl_opt->verify_peer = 1; // 认证对方身份
    ssl_opt->endpoint = HSSL_CLIENT;
    ssl_opt->ca_file = "../cert/ca.crt"; // CA证书
    ssl_opt->crt_file = "../cert/client.crt"; // 客户端证书
    ssl_opt->key_file = "../cert/client_rsa_private.pem"; // 客户端私钥
    hssl_ctx_t ctx = hssl_ctx_new(ssl_opt); // 生成一个hssl_ctx
    if(ctx == NULL) {
        printf("Error: ctx is null\n");
        return 0;
    }
    // ！重点！
    ret = http_client_set_ssl_ctx(cli, ctx); // 将上述hssl_ctx与之前创建的Client绑定
    if (ret != 0) {
        printf("Cert Error: %s : %d\n", http_client_strerror(ret), ret);
        return 0;
    }
    
    // 创建请求
    HttpRequest req;
    req.method = HTTP_POST;
    // req.method = HTTP_GET;
    req.url = "https://127.0.0.1:8080/echo";
    req.headers["Connection"] = "keep-alive";
    req.body = "This is a test request.";
    req.timeout = 10;

    // 发送请求
    HttpResponse resp;
    ret = http_client_send(cli, &req, &resp);

    // 解析
    if (ret != 0) {
        printf("Request Failed: %s : %d\n", http_client_strerror(ret), ret);
    } else {
        printf("%d %s\r\n", resp.status_code, resp.status_message());
        printf("%s\n", resp.body.c_str());
    }

    http_client_del(cli);
}